package com.rpc.core.codec.impl.kryo;


import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.rpc.core.holder.transmit.Request;


public class KryoRequestSerializer extends Serializer<Request>
{
    @Override
    public void write(Kryo kryo, Output output, Request object)
    {
        output.writeInt(object.getId());
        output.writeByte(object.getKey().length());
        output.write(object.getKey().getBytes());
        kryo.writeClassAndObject(output, object.getArgs());
    }

    @Override
    public Request read(Kryo kryo, Input input, Class<Request> type)
    {
        Request rpcRequest = null;
        int id = input.readInt();
        byte keyLength = input.readByte();
        byte[] keyBytes = input.readBytes(keyLength);
        String key = new String(keyBytes);
        Object[] args = (Object[])kryo.readClassAndObject(input);
        rpcRequest = new Request(id, key, args);
        return rpcRequest;
    }
}
